package com.bree.erp.service.stateless;

import java.util.Collection;
import java.util.Collections;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.bree.erp.entity.PoAdjustment;
import com.bree.erp.entity.PurchaseOrder;
import com.bree.erp.service.PoAdjustmentService;

@Stateless
public class PoAdjustmentServiceBean implements PoAdjustmentService {

	@PersistenceContext()
	private EntityManager em;
	
	public PoAdjustmentServiceBean() {
		
	}
	
	public PoAdjustmentServiceBean(EntityManager em) {
		this.em = em;
	}
	
	@Override
	public PoAdjustment read(Long id) {
		return em.find(PoAdjustment.class, id);
	}

	@Override
	public Collection<PoAdjustment> list() {
		return Collections.emptyList();
	}

	@Override
	public PoAdjustment create(Long poId, String description, double amount) {
		
		PurchaseOrder po = em.find(PurchaseOrder.class, poId);
		
		PoAdjustment adjustment = new PoAdjustment();
		adjustment.setPurchaseOrder(po);
		adjustment.setDescription(description);
		adjustment.setAmount(amount);
		
		em.persist(adjustment);
		
		return adjustment;
	}

	@Override
	public void delete(Long id) {
		PoAdjustment entity = read(id);
		
		em.remove(entity);
	}

}
